<?php
    require_once('__common.php');
    login_check();
    $m = getDb('g4_member', $_SESSION['ss_mb_id'], 'mb_id');
    //var_export($_POST);
    extract($_POST);

    if(!$division) {
        alert('오류가 발생했습니다 처음부터 다시 진행해주세요');
    }


    if(in_array('book', $division)) {
            $required = array(
                'method'=>'결재방법',
                'ordername'=>'주문자 이름',
                'ordertel'=>'주문 연락처',
                'orderemail'=>'주문 이메일 ',
                'receiver'=>'수취인 이름',
                'telnum'=>'배송지 연락처',
                'hp'=>'배송지 핸드폰',
                'zip1'=>'우편번호',
                'zip2'=>'우편번호',
                'addr1'=>'주소',
                'addr2'=>'주소',
                'money_sender'=> '입금자명',
            );
    } else {
        $required = array('method'=>'결재방법','money_sender' =>'입금자명');
    }

    requireProcess($required);
    
    $res = array();
    $price = 0;
    foreach($division as $i => $val) {
        if($val == 'course') {
            $c = getDb('course',$div_val[$i], 'no');
            $t = getDb('g4_member', $c['teacher'] , 'mb_no');
            $cb = getDb('book', $c['bookcode'] , 'no');

            $_POST += array('student' => $m['mb_no'], 'tel1' => $_POST['telnum'], 'tel2' => $_POST['hp'], 'course' => $div_val[$i], price => $c['price'], 'bankname' => $_POST['money_sender']);
            $fields =  array('student', 'tel1', 'tel2', 'course', 'bank', 'price', 'bankname', 'memo', 'zip1', 'zip2', 'addr1', 'addr2', 'receiver', 'ordername', 'orderemail');
            putDb('course_order', $_POST, $fields);
            $id = mysql_insert_id();
            $co = getDb('course_order', $id, 'no');
            $price += $co['price'];
            $co['price'] = number_format($co['price']);
            $_c = true;
            //course
        } else if($val == 'book') {
            $b = getDb('book', $_POST['div_val'][$i], 'no');
            list($bookname) = mysql_fetch_row(mysql_query("SELECT name FROM  book WHERE no = {$div_val[$i]}"));
            $_POST += array('student_name' =>$m['mb_name'], 'student' => $m['mb_no'], 'tel1' => $_POST['telnum'], 'tel2' => $_POST['hp'], 'book' => $_POST['div_val'][$i], 'book_name' => $bookname, 'address' =>"{$_POST['zip1']}-{$_POST['zip2']} {$_POST['addr1']} {$_POST['addr2']}", 'bankname' => $_POST['money_sender']);
            $fields =  array('student_name', 'student','book_name', 'tel1', 'tel2', 'book', 'bank', 'price', 'bankname', 'memo', 'address', 'receiver', 'ordername', 'orderemail', 'zip1', 'zip2', 'addr1', 'addr2');
            $price += $b['price'];
            putDb('book_order', $_POST, $fields);
            $id = mysql_insert_id();
            $_b = true;
            if(!$co) {
                    $co = getDb('book_order', $id, 'no');
                    $co['price'] = number_format($co['price']);
            }
        }
    }
//var_export($co);
    $price = number_format($price);

    $tpl->print_('basic');
